home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Games / Hexagonal CA / HexCA.c / TablesUtil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-27  |  5.6 KB  |  174 lines  |  [TEXT/CWIE]

  1. #include "AppTypes.h"
  2. #include "AppConstants.h"
  3. #include "AppGlobals.h"
  4. #include "AppMacros.h"
  5. #include "GridCopyPr.h"
  6.  
  7. #include "TablesUtilPr.h"
  8.  
  9. static Boolean    CreatePositionTable            ( GridDataPtr );
  10. static Boolean    CreateGridArray            ( GridDataPtr );
  11. static Boolean    CreateNeighbourArray        ( GridDataPtr );
  12.  
  13. // InitWindowTables -    call to initialize the window's tables
  14. Boolean InitWindowTables( WindowRef theWindow )
  15. {
  16.     GridDataPtr    refCon = (GridDataPtr)MGetWRefCon( theWindow );
  17.     
  18.     refCon->oldGrid = nil;
  19.     refCon->newGrid = nil;
  20.     refCon->gridMove = nil;
  21.     refCon->neighbours = nil;
  22.     refCon->position = nil;
  23.     
  24.     if( !CreateGridArray( refCon ) )
  25.         return( false );
  26.     if( !CreatePositionTable( refCon ) )
  27.     {
  28.         DisposeTables( refCon );
  29.         return( false );
  30.     }
  31.     if( !CreateNeighbourArray( refCon ) )
  32.     {
  33.         DisposeTables( refCon );        
  34.         return( false );
  35.     }
  36.     return( true );
  37. }
  38.  
  39. // DisposeTables -    call to dispose of a grid's tables
  40. void DisposeTables( GridDataPtr refCon )
  41. {
  42.     if( refCon->oldGrid )
  43.         DisposePtr( (Ptr)refCon->oldGrid );
  44.     if( refCon->newGrid )
  45.         DisposePtr( (Ptr)refCon->newGrid );
  46.     if( refCon->gridMove )
  47.         DisposePtr( (Ptr)refCon->gridMove );
  48.     if( refCon->neighbours )
  49.         DisposePtr( (Ptr)refCon->neighbours );
  50.     if( refCon->position )
  51.         DisposePtr( (Ptr)refCon->position );
  52. }
  53.  
  54. // CreatePositionTable -    call to create the position offset table for the PixMap hex positions
  55. static Boolean CreatePositionTable( GridDataPtr refCon )
  56. {
  57.     long            rowBytes = (long)((**MGetGWorldPixMap( refCon->theGWorld )).rowBytes & 0x3FFF);
  58.     long            countx, county, offsetx, offsety, total = 0;
  59.     Boolean        even = true;
  60.     short        hexMidPoint, hexSize;
  61.     unsigned char    *baseAddr = (unsigned char *)GetPixBaseAddr( MGetGWorldPixMap( refCon->theGWorld ) );
  62.     
  63.     if( !(refCon->position = (unsigned char **)NewPtr( sizeof( unsigned char * ) * refCon->gridTotalSize )) )
  64.         return( false );
  65.     
  66.     refCon->rowBytes = rowBytes;
  67.     switch( refCon->scale )
  68.     {
  69.         case kHexSize:
  70.             hexMidPoint = kHexMidPoint;
  71.             hexSize = kHexSize;
  72.             break;
  73.         case kMiniHexSize:
  74.             hexMidPoint = kMiniHexMidPoint;
  75.             hexSize = kMiniHexSize;
  76.             break;
  77.         case kTinyHexSize:
  78.             hexMidPoint = kTinyHexMidPoint;
  79.             hexSize = kTinyHexSize;
  80.             break;
  81.     }
  82.     
  83.     offsetx = -(**MGetGWorldPixMap( refCon->theGWorld )).bounds.left + (refCon->theGWorld->portRect.right - refCon->gridHSize * hexSize - hexMidPoint + 1) / 2;
  84.     offsety = -(**MGetGWorldPixMap( refCon->theGWorld )).bounds.top + (refCon->theGWorld->portRect.bottom - refCon->gridVSize * (hexSize - hexMidPoint / 2 + 1) - hexMidPoint + hexMidPoint / 2 + 1) / 2;
  85.     
  86.     for( county = 0; county < refCon->gridVSize; county++ )
  87.     {
  88.         for( countx = 0; countx < refCon->gridHSize; countx++, total++ )
  89.             *(refCon->position + total) = baseAddr + offsetx + countx * hexSize + rowBytes * (offsety + county * (hexSize - hexMidPoint / 2 + 1)) + (even ? hexMidPoint : 0);
  90.         even = !even;
  91.     }
  92.     
  93.     return( true );
  94. }
  95.     
  96. // CreateGridArray -    call to create a grid array and the asociated data
  97. static Boolean CreateGridArray( GridDataPtr refCon )
  98. {
  99.     if( !(refCon->newGrid = (Boolean *)NewPtr( sizeof( Boolean ) * refCon->gridTotalSize )) )
  100.         return( false );
  101.     if( !(refCon->oldGrid = (Boolean *)NewPtrClear( sizeof( Boolean ) * refCon->gridTotalSize )) )
  102.         return( false );
  103.     if( !(refCon->gridMove = (RBlockDataPtr)NewPtr( sizeof( RBlockData ) )) )
  104.         return( false );
  105.     SetRBlockData( sizeof( Boolean ) * refCon->gridTotalSize, refCon->gridMove );
  106.     return( true );
  107. }
  108.  
  109. // CreateNeighbourArray -    creates and sets the look-up tables for neighbour cells (must be called _after_ the gids are created!)
  110. static Boolean CreateNeighbourArray( GridDataPtr refCon )
  111. {
  112.     long            countx, county, totalx = refCon->gridHSize, totaly = refCon->gridVSize;
  113.     Boolean        *oldSrc = refCon->oldGrid, *oldGrid = refCon->oldGrid;
  114.     NeighbourPtr    neighbours;
  115.     Boolean        even = true;
  116.     
  117.     if( !(refCon->neighbours = (NeighbourPtr)NewPtr( sizeof( NeighbourRec ) * refCon->gridTotalSize )) )
  118.         return( false );
  119.     
  120.     neighbours = refCon->neighbours;
  121.     for( county = 0; county < totaly; county++ )
  122.     {
  123.         for( countx = 0; countx < totalx; countx++, oldSrc++, neighbours++ )
  124.         {
  125.             if( county != 0 && county != totaly - 1 && countx != 0 && countx != totalx - 1 )
  126.             {                
  127.                 neighbours->neighbour[0] = oldSrc - 1;
  128.                 neighbours->neighbour[1] = oldSrc + 1;
  129.                 if( even )
  130.                 {
  131.                     neighbours->neighbour[2] = oldSrc - totalx;
  132.                     neighbours->neighbour[3] = oldSrc - totalx + 1;
  133.                     neighbours->neighbour[4] = oldSrc + totalx;
  134.                     neighbours->neighbour[5] = oldSrc + totalx + 1;
  135.                 }
  136.                 else
  137.                 {
  138.                     neighbours->neighbour[2] = oldSrc - totalx - 1;
  139.                     neighbours->neighbour[3] = oldSrc - totalx;
  140.                     neighbours->neighbour[4] = oldSrc + totalx - 1;
  141.                     neighbours->neighbour[5] = oldSrc + totalx;
  142.                 }
  143.             }
  144.             else
  145.             {
  146.                 long    offxn, offxp, offyn, offyp, offym = county * totalx;
  147.                 
  148.                 if( countx != 0 ) offxn = countx - 1; else offxn = totalx - 1;
  149.                 if( countx != totalx - 1 ) offxp = countx + 1; else offxp = 0;
  150.                 if( county != 0 ) offyn = offym - totalx; else offyn = (totaly - 1) * totalx;
  151.                 if( county != totaly - 1 ) offyp = offym + totalx; else offyp = 0;
  152.                 
  153.                 neighbours->neighbour[0] = oldGrid + offym + offxn;
  154.                 neighbours->neighbour[1] = oldGrid + offym + offxp;
  155.                 if( even )
  156.                 {
  157.                     neighbours->neighbour[2] = oldGrid + offyn + countx;
  158.                     neighbours->neighbour[3] = oldGrid + offyn + offxp;
  159.                     neighbours->neighbour[4] = oldGrid + offyp + countx;
  160.                     neighbours->neighbour[5] = oldGrid + offyp + offxp;
  161.                 }
  162.                 else
  163.                 {
  164.                     neighbours->neighbour[2] = oldGrid + offyn + countx;
  165.                     neighbours->neighbour[3] = oldGrid + offyn + offxn;
  166.                     neighbours->neighbour[4] = oldGrid + offyp + countx;
  167.                     neighbours->neighbour[5] = oldGrid + offyp + offxn;
  168.                 }
  169.             }
  170.         }
  171.         even = !even;
  172.     }
  173.     return( true );
  174. }